/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.controller.dialog;

import cn.ac.nercis.pes.community.component.dialog.AlertDialog;
import cn.ac.nercis.pes.community.component.dialog.AlertMode;
import cn.ac.nercis.pes.community.component.dialog.AlertType;
import cn.ac.nercis.pes.community.component.dialog.PageDialog;
import cn.ac.nercis.pes.community.controller.MainController;
import cn.ac.nercis.pes.community.controller.manage.AboutController;
import cn.ac.nercis.pes.community.controller.manage.PathManageController;
import cn.ac.nercis.pes.community.support.FXMLController;
import cn.ac.nercis.pes.community.support.controls.JFXPopup;
import cn.ac.nercis.pes.community.utils.AppStore;
import cn.ac.nercis.pes.community.utils.GuiStore;
import cn.ac.nercis.pes.community.utils.SpringUtils;
import cn.ac.nercis.pes.community.views.dialog.ModifyPwdView;
import cn.ac.nercis.pes.community.views.login.LoginView;
import cn.ac.nercis.pes.community.views.manage.AboutView;
import cn.ac.nercis.pes.community.views.manage.PathManageView;
import javafx.beans.property.SimpleStringProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import lombok.extern.slf4j.Slf4j;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * 用户信息弹框控制器
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
@FXMLController
public class UserInfoController implements Initializable {

    @FXML
    private Label userLabel;
    @FXML
    private Label roleLabel;

    private StackPane page;
    private JFXPopup parentPopup;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }

    public void initData(JFXPopup popup, StackPane page){
        this.parentPopup = popup;
        this.page = page;
        userLabel.textProperty().bind(new SimpleStringProperty(AppStore.getCurrentUser().getDisplayName()));
        roleLabel.textProperty().bind(new SimpleStringProperty(AppStore.getCurrentUser().getRoleName()));
    }

    @FXML
    public void onModifyPwdClick(){
        this.parentPopup.hide();
        var pageDialog = PageDialog.builder()
                .withIcon("dialog-edit-icon")
                .withTitle("修改密码")
                .withContent(SpringUtils.getBean(ModifyPwdView.class).getView())
                .build();
        SpringUtils.getBean(ModifyPwdController.class).initData(pageDialog);
        pageDialog.show(this::saveModifyPwd);
    }

    @FXML
    public void onLogoutClick(){
        this.parentPopup.hide();
        AlertDialog alertDialog = AlertDialog.builder()
                .withMode(AlertMode.CONFIRM)
                .withType(AlertType.WARN)
                .withTitle("操作提示")
                .withContent("确认退出当前登录用户吗？")
                .withContainer(this.page)
                .build();
        alertDialog.show((confirm)->{
            if(!confirm){
                return;
            }
            AppStore.setCurrentUser(null);
            AppStore.clearCurrentSystem();
            GuiStore.getScene().setRoot(SpringUtils.getBean(LoginView.class).getView());
        });
    }

    @FXML
    public void onPzClick(){
        this.parentPopup.hide();
        SpringUtils.getBean(MainController.class).setCenter(SpringUtils.getBean(PathManageView.class).getView());
        SpringUtils.getBean(PathManageController.class).initializeData();
    }

    @FXML
    public void onGyClick(){
        this.parentPopup.hide();
        SpringUtils.getBean(MainController.class).setCenter(SpringUtils.getBean(AboutView.class).getView());
        SpringUtils.getBean(AboutController.class).initializeData();
    }

    @FXML
    public void onExitClick(){
        this.parentPopup.hide();
        AlertDialog alertDialog = AlertDialog.builder()
                .withMode(AlertMode.CONFIRM)
                .withType(AlertType.WARN)
                .withTitle("操作提示")
                .withContent("确认关闭应用吗？")
                .build();
        alertDialog.show((confirm)->{
            if(!confirm){
                return;
            }
            GuiStore.getStage().close();
            SpringUtils.getApplicationContext().close();
            System.exit(0);
        });
    }

    private void saveModifyPwd(){
        SpringUtils.getBean(ModifyPwdController.class).onSaveClick();
    }
}